package com.sdy.hospitalguard.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sdy.hospitalguard.biz.model.VisitAppointment;
import com.sdy.hospitalguard.biz.model.VisitTimeout;
import com.sdy.hospitalguard.biz.mapper.VisitTimeoutMapper;
import com.sdy.hospitalguard.biz.service.*;
import com.sdy.mvc.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 探视超时消息信息存放表 服务实现类
 * </p>
 *
 * @author zzq
 * @since 2020-06-01
 */
@Slf4j
@Service
public class VisitTimeoutServiceImpl extends BaseServiceImpl<VisitTimeout> implements VisitTimeoutService {

    @Autowired
    private VisitTimeoutMapper visitTimeoutMapper;
    @Autowired
    private VisitAppointmentService visitAppointmentService;
    @Autowired
    private HospitalUserService hospitalUserService;
    @Autowired
    private SickbedService sickbedService;
    @Autowired
    private PatientInformationService patientInformationService;

    @Override
    public void saveTimeout() {
        Date date = new Date();
        QueryWrapper<VisitAppointment> wrapper = new QueryWrapper<>();
        wrapper.eq("appointment_state", 0);
        wrapper.ne("visit_state", 3);
        wrapper.isNotNull("practical_start_time");
        wrapper.isNull("practical_end_time");
        wrapper.lt("visit_end_time", date);
        List<VisitAppointment> list = visitAppointmentService.list(wrapper);
        Integer size = list.size();
        for (int i=0; i<size; i++ ) {
            VisitAppointment visitAppointment = list.get(i);
            String sickbed = sickbedService.getById(patientInformationService
                    .getById(visitAppointment.getPatientId()).getSickbedId()).getSickbedNumber();
            String name = hospitalUserService.getById(visitAppointment.getUserId()).getName();
            Date timeout = visitAppointment.getVisitEndTime();
            Integer visitId = visitAppointment.getId();
            Integer areaId = sickbedService.getById(patientInformationService
                    .getById(visitAppointment.getPatientId()).getSickbedId()).getAreaId();
            if (list(new QueryWrapper<VisitTimeout>().eq("visit_id", visitId)).size() > 0) {

            } else {
                save(new VisitTimeout()
                        .setVisitId(visitId)
                        .setAreaId(areaId)
                        .setSickbedNumber(sickbed)
                        .setVisitName(name)
                        .setTimeout(timeout)
                        .setCreateTime(new Date())
                        .setStatus(0));
                visitAppointmentService.update(new UpdateWrapper<VisitAppointment>()
                        .eq("id", visitId)
                        .set("visit_state", 2));
            }
        }
    }

}
